<?php

/**
 * Description of HttpUtil
 *
 * @author Administrator
 */
require_once 'requests/Requests.php';
Requests::register_autoloader();

class HttpUtil {

    /**
     * 通用请求
     * @param type $url         链接地址
     * @param type $rawheaders  头部信息
     * @param type $data        post数据
     * @param type $isPost      是否post方式提交，默认true
     * @param type $retry       是否重试
     * @return type 请求返回信息
     */
    public static function request($url, $rawheaders, $data, $isPost = true, $proxy = false, $retry = false) {
        $options = [];
        $proxy && $options['proxy'] = '127.0.0.1:8888';

        $options['timeout'] = 1.5;
        try {
            $response = Requests::request($url, $rawheaders, $data, ($isPost ? Requests::POST : Requests::GET), $options);
        } catch (Exception $ex) {
            $response = false;
        }

        if (!$retry) {
            return $response;
        }

        $retryTimes = 1;
        while ($response === false && $retryTimes <= 3) {
            try {
                $response = Requests::request($url, $rawheaders, $data, ($isPost ? Requests::POST : Requests::GET), $options);
            } catch (Exception $ex) {
                $response = false;
            }
            $retryTimes ++;
        }
        return $response;
    }

    /**
     * 登陆类型获取返回headers
     * @param type $url         请求地址
     * @param type $rawheaders  头部信息
     * @param type $data        post参数
     * @param type $isPost      是否post方式
     * @return type 请求返回信息
     */
    public static function getHeaders($url, $rawheaders, $data, $isPost = true, $proxy = false, $retry = false) {
        $options = [];
        $proxy && $options['proxy'] = '127.0.0.1:8888';

        $options['timeout'] = 1.5;
        try {
            $response = Requests::request($url, $rawheaders, $data, ($isPost ? Requests::POST : Requests::GET), $options);
        } catch (Exception $ex) {
            $response = false;
        }

        if (!$retry) {
            return isset($response->headers) ? $response->headers : false;
        }

        $retryTimes = 1;
        while ($response === false && $retryTimes <= 3) {
            try {
                $response = Requests::request($url, $rawheaders, $data, ($isPost ? Requests::POST : Requests::GET), $options);
            } catch (Exception $ex) {
                $response = false;
            }
            $retryTimes ++;
        }
        return isset($response->headers) ? $response->headers : false;
    }

    /**
     * 发起一个数据配置请求-取得headers信息
     * @param type $site
     * @param type $proxy
     * @param type $retry
     */
    public static function cfg_request_headers($site, $proxy = false, $retry = false) {
        $headers = self::getHeaders($site['url'], $site['rawheaders'], isset($site['data']) ? $site['data'] : '', $site['isPost'], $proxy, $retry);
        return $headers;
    }

    /**
     * 发起一个数据配置请求
     * @param type $site    配置化请求配置
     * @return type
     */
    public static function cfg_request($site, $proxy = false, $retry = false) {
        $response = self::request($site['url'], $site['rawheaders'], isset($site['data']) ? $site['data'] : '', $site['isPost'], $proxy, $retry);
        $res = isset($response->body) ? $response->body : false;
        unset($response);
        return $res;
    }

    /**
     * 发起一个数据配置请求
     * @param type $site    配置化请求配置
     * @return type
     */
    public static function cfg_request_options($site, $options = []) {
        $response = self::request_options($site['url'], $site['rawheaders'], isset($site['data']) ? $site['data'] : '', $site['isPost'], $options);
        return isset($response->body) ? $response->body : false;
    }

    /**
     * 通用请求
     * @param type $url         链接地址
     * @param type $rawheaders  头部信息
     * @param type $data        post数据
     * @param type $isPost      是否post方式提交，默认true
     * @param type $options     
     * @return type 请求返回信息
     */
    public static function request_options($url, $rawheaders, $data, $isPost = true, $options = []) {
        try {
            $response = Requests::request($url, $rawheaders, $data, ($isPost ? Requests::POST : Requests::GET), $options);
        } catch (Exception $ex) {
            $response = $ex;
        }
        return $response;
    }

    /**
     * 发起多个数据配置请求
     * @param type $sites
     * @param type $proxy
     * @return type
     */
    public static function cfg_request_mutil($sites, $proxy = false) {
        $options = [];
        $proxy && $options['proxy'] = '127.0.0.1:8888';
        $options['timeout'] = 1.5;

        $requests = [];
        foreach ($sites as $key => $site) {
            $requests[$key] = [
                'url' => $site['url'],
                'headers' => $site['rawheaders'],
                'data' => isset($site['data']) ? $site['data'] : '',
                'type' => ($site['isPost'] ? Requests::POST : Requests::GET)
            ];
        }

        $responseArr = Requests::request_multiple($requests, $options);
        $result = [];
        foreach ($responseArr as $key => $response) {
            $result[$key] = isset($response->body) ? $response->body : false;
        }
        return $result;
    }

    /**
     * 获取跳转后的真实地址
     * @param string $url   跳转前地址
     * @param int $index    location 为数组时取第几个值
     * @return string       真实地址
     */
    public static function real_url($url, $index = 0, $options = []) {
        if (isset($options['proxy'])) {
            $default_opts = array(
                'http' => array(
                    'proxy' => $options['proxy']
                )
            );
            stream_context_set_default($default_opts);
        }
        $headers = @get_headers($url, TRUE);
        if (empty($headers['Location'])) {
            return $url;
        }

        if (is_array($headers['Location']) && isset($headers['Location'][$index]) && is_string($headers['Location'][$index])) {
            return $headers['Location'][$index];
        }

        if (is_string($headers['Location'])) {
            return $headers['Location'];
        }
        return $url;
    }

}
